package taskmanage

import (
	//"fmt"
	"jiabo/appframework/libs/db"
	"labix.org/v2/mgo"
	"labix.org/v2/mgo/bson"
)

type Orgnization struct {
	Id      bson.ObjectId `bson:"_id,omitempty" json:"-"`
	Oid     string        `bson:"oid" json:"oid"`
	OrgName string        `bson:"orgName" json:"orgName"`
	Owner   string        `bson:"owner" json:"owner"`
	Desc    string        `bson:"desc" json:"desc"`
}

//判断数据库中是否存在本Oid的部门
func (org *Orgnization) CheckIfOrgnizationExist() (result bool, searchErr error) {
	searchErr = nil
	searchResults := Orgnization{}

	query := func(c *mgo.Collection) error {
		fn := c.Find(bson.M{"oid": org.Oid}).One(&searchResults)
		return fn
	}
	search := func() error {
		return db.WithCollection("orgnizations", query)
	}
	err := search()
	if err != nil {
		searchErr = err
	}

	if searchResults.Id != "" {
		result = true
	} else {
		result = false
	}
	return
}
func (org *Orgnization) CheckIfOrgnizationExistbyName() (result bool, searchErr error) {
	searchErr = nil
	searchResults := Orgnization{}

	query := func(c *mgo.Collection) error {
		fn := c.Find(bson.M{"orgName": org.OrgName}).One(&searchResults)
		return fn
	}
	search := func() error {
		return db.WithCollection("orgnizations", query)
	}
	err := search()
	if err != nil {
		searchErr = err
	}

	if searchResults.Id != "" {
		result = true
	} else {
		result = false
	}
	return
}
func (org *Orgnization) Save() error {
	//判断如果部门已经存在，就更新，如果不存在就插入。
	ifOrgnizationExist, err := org.CheckIfOrgnizationExist()
	if err != nil {
		//return err
	}
	if ifOrgnizationExist {
		update := func(c *mgo.Collection) error {
			fn := c.Update(bson.M{"oid": org.Oid}, org)
			return fn
		}

		updateExec := func() error {
			return db.WithCollection("orgnizations", update)
		}

		err := updateExec()
		return err
	} else {
		insert := func(c *mgo.Collection) error {

			fn := c.Insert(org)
			return fn
		}

		insertExec := func() error {
			return db.WithCollection("orgnizations", insert)
		}

		err := insertExec()
		return err
	}
	return nil
}

func (org *Orgnization) Delete() error {
	//判断如果部门已经存在，就更新，如果不存在就插入。
	ifOrgnizationExist, err := org.CheckIfOrgnizationExist()
	if err != nil {
		//return err
	}
	if ifOrgnizationExist {
		delet := func(c *mgo.Collection) error {
			fn := c.Remove(bson.M{"oid": org.Oid})
			return fn
		}

		deleteExec := func() error {
			return db.WithCollection("orgnizations", delet)
		}

		err := deleteExec()
		return err
	}
	return nil
}

func GetOrgnizationByOid(oid string) (org Orgnization, searchErr error) {
	searchErr = nil
	//department = nil
	searchResults := Orgnization{}

	query := func(c *mgo.Collection) error {
		fn := c.Find(bson.M{"oid": oid}).One(&searchResults)
		return fn
	}
	search := func() error {
		return db.WithCollection("orgnizations", query)
	}
	err := search()
	if err != nil {
		searchErr = err
		return
	}

	if searchResults.Id != "" {
		org = searchResults
		return
	}
	return
}

func GetOrgnizationByOids(oids []string) (org []Orgnization, searchErr error) {
	searchErr = nil
	//department = nil
	searchResults := []Orgnization{}

	query := func(c *mgo.Collection) error {
		fn := c.Find(bson.M{"oid": bson.M{"$in": oids}}).All(&searchResults)
		return fn
	}
	search := func() error {
		return db.WithCollection("orgnizations", query)
	}
	err := search()
	if err != nil {
		searchErr = err
		return
	}

	org = searchResults
	return
}

func GetOrgnizationByName(orgName string) (org Orgnization, searchErr error) {
	searchErr = nil
	//department = nil
	searchResults := Orgnization{}

	query := func(c *mgo.Collection) error {
		fn := c.Find(bson.M{"orgName": orgName}).One(&searchResults)
		return fn
	}
	search := func() error {
		return db.WithCollection("orgnizations", query)
	}
	err := search()
	if err != nil {
		searchErr = err
		return
	}

	if searchResults.Id != "" {
		org = searchResults
		return
	}
	return
}

func GetOrgnizationByNameContainsStr(str string) (org []Orgnization, searchErr error) {
	searchErr = nil
	//department = nil
	searchResults := []Orgnization{}

	query := func(c *mgo.Collection) error {
		fn := c.Find(bson.M{"orgName": bson.RegEx{str, "i"}}).All(&searchResults)
		return fn
	}
	search := func() error {
		return db.WithCollection("orgnizations", query)
	}
	err := search()
	if err != nil {
		searchErr = err
		return
	}

	org = searchResults
	return
}
